//
//  GetWeatherHandle.m
//  WeatherGuide
//
//  Created by lanou3g on 15/12/17.
//  Copyright © 2015年 李明. All rights reserved.
//

#import "GetWeatherHandle.h"
#import <UIKit/UIKit.h>
#import "CityModel.h"
@interface GetWeatherHandle ()<NSXMLParserDelegate>

@property (nonatomic,strong) NSMutableArray *array;

@property (nonatomic,strong) NSMutableArray *arrayCityInfo;

@property (nonatomic,copy) NSString *strCity;

@property (nonatomic,strong) NSMutableDictionary *dictProvince;

@property (nonatomic,strong) NSMutableDictionary *dictCity;

@property (nonatomic,strong) NSMutableArray *arrayCity;

//@property (nonatomic,strong) CityModel *cityModel;


@end

GetWeatherHandle *getWeatherHandle = nil;
@implementation GetWeatherHandle

+ (instancetype)sharedHandle
{
    return [[self alloc] init];
}

+ (instancetype)allocWithZone:(struct _NSZone *)zone
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if (getWeatherHandle == nil) {
            getWeatherHandle = [super allocWithZone:zone];
            getWeatherHandle.arrayLifeIndex = @[@"舒适度指数",@"洗车指数",@"穿衣指数",@"感冒指数",@"运动指数",@"旅游指数",@"紫外线指数"];
//            getWeatherHandle.cityDic = @{@"北京":@[@"北京",@"海淀",@"朝阳",@"顺义",@"怀柔",@"通州",@"昌平",@"延庆",@"丰台",@"石景山",@"大兴",@"房山",@"密云",@"门头沟",@"平谷"],
//                                         @"上海":@[@"上海",@"闵行",@"宝山",@"嘉定",@"浦东南汇",@"金山",@"青浦",@"松江",@"奉贤",@"崇明",@"徐家汇",@"浦东"],
//                                         @"天津":@[@"天津",@"武清",@"宝坻",@"东丽",@"西青",@"北辰",@"宁河",@"han",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@""]
//                                         };
        }
    });
    return getWeatherHandle;
}

- (NSArray *)arrayLifeIndexImage
{
    if (_arrayLifeIndexImage == nil) {
        _array = [NSMutableArray array];
        for (int i = 1; i < 8; i++) {
            UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.png",i]];
            [_array addObject:image];
        }
        _arrayLifeIndexImage = [NSArray arrayWithArray:_array];
    }
    return _arrayLifeIndexImage;
}



- (void)getData
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
        NSLog(@"1");
        NSString *filePath = [[NSBundle mainBundle] pathForResource:@"city" ofType:@"xml"];
        NSURL *url = [NSURL fileURLWithPath:filePath];
        NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:url];
        parser.delegate = self;
        [parser parse];
    });
 

}

- (void)parserDidStartDocument:(NSXMLParser *)parser
{
    NSLog(@"111");
    NSLog(@"%@",parser);
    self.arrayCity = [NSMutableArray array];

}

- (void)parserDidEndDocument:(NSXMLParser *)parser
{
    NSMutableSet *setProvice = [NSMutableSet set];
    
    NSMutableDictionary *dictProvice = [NSMutableDictionary dictionary];
    
    for (CityModel *model in self.arrayCity) {
//        NSLog(@"%@",array[4]);
        [setProvice addObject:model.province];
    
    }
//    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%@"];
    for (NSString *proviceName in setProvice) {
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"province CONTAINS %@",proviceName];
        NSArray *arraycity = [self.arrayCity filteredArrayUsingPredicate:predicate];
        
        NSMutableDictionary *dictCity = [NSMutableDictionary dictionary];
        [dictProvice setObject:dictCity forKey:proviceName];
        
     
        NSMutableSet *setCity = [NSMutableSet set];
        for (CityModel *model in arraycity) {
           
            [setCity addObject:model.city];
        }
        for (NSString *cityName in setCity) {
            NSPredicate *predicateCity;
            NSMutableDictionary *dicCounties = [NSMutableDictionary dictionary];
            if (![cityName isEqualToString:@"北京"] && ![cityName isEqualToString:@"上海"]&& ![cityName isEqualToString:@"天津"]&& ![cityName isEqualToString:@"重庆"]&& ![cityName isEqualToString:@"香港"]&& ![cityName isEqualToString:@"澳门"]) {
                predicateCity = [NSPredicate predicateWithFormat:@"city CONTAINS %@",cityName];
                NSArray *arraycounties = [arraycity filteredArrayUsingPredicate:predicateCity];
                
                [dictCity setObject:dicCounties forKey:cityName];
                for (CityModel *model in arraycounties) {
                    [dicCounties setObject:model.countiesENG forKey:model.countiesENG];
                }
            }else{
//                predicateCity = [NSPredicate predicateWithFormat:@"countiesENG CONTAINS %@",cityName];
                [dicCounties setObject:cityName forKey:cityName];
            }
        
           
        }
        
    }
    self.dicCityInfo = [NSDictionary dictionaryWithDictionary:dictProvice];

    dispatch_async(dispatch_get_main_queue(), ^{
        NSLog(@"结束");
    });
    
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(nullable NSString *)namespaceURI qualifiedName:(nullable NSString *)qName attributes:(NSDictionary<NSString *, NSString *> *)attributeDict
{
    if ([elementName isEqualToString:@"tr"]) {
        self.arrayCityInfo = [NSMutableArray arrayWithCapacity:4];
    }

}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(nullable NSString *)namespaceURI qualifiedName:(nullable NSString *)qName
{
    if ([elementName isEqualToString:@"td"]) {
        [self.arrayCityInfo addObject:self.strCity];
    }else if ([elementName isEqualToString:@"tr"]){
        CityModel *cityModle = [[CityModel alloc] initWithCityID:self.arrayCityInfo[0] countiesCN:self.arrayCityInfo[1] countiesENG:self.arrayCityInfo[2] city:self.arrayCityInfo[3] province:self.arrayCityInfo[4]];
        [self.arrayCity addObject:cityModle];
        

        
        
    }

}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    self.strCity = string;

}

@end
